package com.myy.purchase.controller;

import com.myy.common.annotation.Log;
import com.myy.common.exception.FebsException;
import com.myy.purchase.entity.Purchase;
import com.myy.purchase.service.IPurchaseService;
import com.myy.common.controller.BaseController;
import com.myy.common.entity.FebsResponse;
import com.myy.common.entity.QueryRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("warehouse")
public class WarehouseController extends BaseController {

    @Autowired
    private IPurchaseService  purchaseService;

    @GetMapping("/list")
    @RequiresPermissions("warehouse:view")
    public FebsResponse logList(Purchase purchase, QueryRequest request) {
        Map<String, Object> dataTable = getDataTable(this.purchaseService.findPurchase(purchase, request));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("新增入库")
    @PostMapping("/add")
    @RequiresPermissions("warehouse:add")
    public FebsResponse addUser(@Valid Purchase purchase) throws FebsException {
        log.info(" ---参数：{} ",purchase);
        try {
            purchaseService.createPurchase(purchase);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "入库失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
